package com.example.zzpneo4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    public static Date transferDate(String strDate) {
        String result = "";
        if (strDate == null || strDate.isEmpty()) {
            return null;
        }
        if (strDate.matches("\\d{2}.\\d{2}.\\d{2}|\\d{2}.\\d{2}.\\d{4}")) {
            String[] arrDate = strDate.split("\\.");
            String year = arrDate[2];
            if (arrDate[2].length() == 2) {
                year = "20" + arrDate[2];
            }
            result = year + "-" + arrDate[0] + "-" + arrDate[1];
        } else {
            String[] arrDate = strDate.split(",");
            String[] arrMonth = arrDate[0].split(" ");
            String day = arrMonth[1];
            if (arrMonth[1].length() == 1) {
                day = "0" + arrMonth[1];
            }
            switch (arrMonth[0]) {
                case "January":
                    result = arrDate[1].trim() + "-" + "01" + "-" + day;
                    break;
                case "February":
                    result = arrDate[1].trim() + "-" + "02" + "-" + day;
                    break;
                case "March":
                    result = arrDate[1].trim() + "-" + "03" + "-" + day;
                    break;
                case "April":
                    result = arrDate[1].trim() + "-" + "04" + "-" + day;
                    break;
                case "May":
                    result = arrDate[1].trim() + "-" + "05" + "-" + day;
                    break;
                case "June":
                    result = arrDate[1].trim() + "-" + "06" + "-" + day;
                    break;
                case "July":
                    result = arrDate[1].trim() + "-" + "07" + "-" + day;
                    break;
                case "August":
                    result = arrDate[1].trim() + "-" + "08" + "-" + day;
                    break;
                case "September":
                    result = arrDate[1].trim() + "-" + "09" + "-" + day;
                    break;
                case "October":
                    result = arrDate[1].trim() + "-" + "10" + "-" + day;
                    break;
                case "November":
                    result = arrDate[1].trim() + "-" + "11" + "-" + day;
                    break;
                case "December":
                    result = arrDate[1].trim() + "-" + "12" + "-" + day;
                    break;
            }
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取某一个时间戳凌晨
     *
     * @param d
     * @author 张宇
     */
    public static Long getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
                0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取某一个时间戳结束时间(23:59)
     *
     * @param d
     * @author 张宇
     */
    public static Long getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
                59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

}
